<template>
	<view class="card" @click="handleDetail">
		<view class="top">
			<view class="left" @click.stop="handleUser">
				<image class="avatar" :src="detail.avatarURL" mode="aspectFill"></image>
				<view class="name">{{ detail.username }}</view>
			</view>
			<view class="right">
				<view class="time">{{ detail.createDate }}</view>
				<view class="category">{{ detail.category }}</view>
			</view>
		</view>
		<view class="title">{{ detail.title }}</view>
		<view class="content">{{ detail.content }}</view>
		<view class="images" v-if="detail.images !== ''">
			<image class="image" :lazy-load="true" v-for="(item, index) in JSON.parse(detail.images).slice(0, 4)"
				:key="index" mode="aspectFill" :src="item" @click.stop="previewImage(index)" />
		</view>
		<view class="footer">
			<view class="like">
				<view v-if="!detail.liked" style="scale: 0.8" @click.stop="handleLike">
					<uni-icons class="t-icon t-icon-dianzan"></uni-icons>
				</view>
				<view v-else style="scale: 0.8" @click.stop="handleDislike">
					<uni-icons class="t-icon t-icon-dianzan1"></uni-icons>
				</view>
				{{ detail.likeCount }}
			</view>
			<view class="comment">
				<view style="display: flex; align-items: center">
					<uni-icons class="t-icon t-icon-xiaoxi" style="scale: 0.8"></uni-icons>
					{{ detail.commentCount }}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onShow
	} from '@dcloudio/uni-app'
	import {
		defineProps,
		ref
	} from 'vue'
	import {
		CircleDislike,
		CircleLike,
	} from '@/apis/circle'

	const props = defineProps({
		detail: Object
	})
	const commentRef = ref()
	const commentList = ref()

	// 图片预览
	function previewImage(e) {
		uni.previewImage({
			current: e,
			urls: JSON.parse(props.detail.images),
			loop: false
		})
	}
	// 跳转详情
	function handleDetail() {
		uni.navigateTo({
			url: `/pages/circle/detail/detail?id=${props.detail.circleID}`
		})
	}
	// 跳转用户详情
	function handleUser() {
		uni.navigateTo({
			url: `/pages/my/contact/detail/detail?userId=${props.detail.userID}`
		})
	}
	// 点赞
	function handleLike() {
		CircleLike({
				circleID: props.detail.circleID
			})
			.then((res) => {
				uni.showToast({
					title: res.message,
					icon: 'success'
				})
			})
			.finally(() => {
				// refresh()
				props.detail.likeCount++
				props.detail.liked = true
			})
		console.log('like')
	}
	// 取消点赞
	function handleDislike() {
		CircleDislike({
				circleID: props.detail.circleID
			})
			.then((res) => {
				uni.showToast({
					title: res.message,
					icon: 'success'
				})
			})
			.finally(() => {
				// refresh()
				props.detail.likeCount--
				props.detail.liked = false
			})
	}
</script>

<style scoped lang="scss">
	.card {
		// height: 320rpx;
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		border-top: 1px solid #0001;
		border-bottom: 1px solid #0001;
		width: 100%;

		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 100%;

			.left {
				display: flex;
				align-items: center;
				gap: 20rpx;

				.avatar {
					height: 80rpx;
					width: 80rpx;
					aspect-ratio: 1/1;
					border-radius: 50%;
					// border: 1px solid #ccc;
				}

				.name {
					font-size: 32rpx;
					font-weight: bold;
					// font-family: SmileySans;
					color: $my-wzut-icon-primary;
				}
			}

			.right {
				font-size: 24rpx;
				text-align: right;

				.category {
					flex: 1;
					color: #666;
					font-size: 20rpx;
				}
			}
		}

		.title {
			margin-top: 10rpx;
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-weight: bold;
			line-height: 60rpx;
			font-size: 36rpx;
			gap: 10rpx;
			overflow: hidden;
			word-break: break-all;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.content {
			font-size: 24rpx;
			display: -webkit-box;
			overflow: hidden;
			text-overflow: ellipsis;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			color: #333;
			margin-bottom: 20rpx;
		}

		.images {
			overflow: hidden;
			white-space: nowrap;
			display: grid;
			grid-template-columns: repeat(4, 1fr);
			gap: 10rpx;

			.image {
				width: 100%;
				height: 200rpx;
				aspect-ratio: 1/1;

				&:last-child {
					margin-right: 0;
				}
			}
		}

		.footer {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			gap: 20rpx;
			margin-top: 10rpx;
			font-size: 24rpx;

			.like {
				display: flex;
				align-items: center;
				// gap: 10rpx;
			}

			.comment {
				display: flex;
				align-items: center;

				// gap: 10rpx;
				.avatar {
					max-width: 50rpx;
					border-radius: 50%;
					margin-left: -40rpx;
					backdrop-filter: blur(10rpx);

					&:first-child {
						margin-left: 0;
					}
				}
			}
		}
	}
</style>
